package com.vector.demo.controller.teacher;

import com.vector.demo.pojo.Task;
import com.vector.demo.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.TaskNamespaceHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/task")
public class TaskController {
    @Autowired
    TaskService taskService;

    @RequestMapping("/queryAllTask")
    public void queryAllTask() {
        List<Task> taskList = taskService.queryAllTask();
        System.out.println(taskList);
    }

    @RequestMapping("/toPublishJob")
    public String toPublishJob() {
        return "teacher/t_publish";
    }

    @ResponseBody
    @RequestMapping("/doPublishJob")
    public String doPublishJob(String title, String contents, String date, String teacherName, String className) {
        int status = 1;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(new Date()));
        System.out.println(date);
        Task task = new Task();
        task.setTask_name(title);
        task.setTask_content(contents);
        task.setTask_sta_time(df.format(new Date()));
        task.setTask_end_time(date);
        task.setCla_name(className);
        task.setT_name(teacherName);
        task.setStatus(status);
        int i = taskService.addTask(task);
        System.out.println(i + "添加成功");
        return "1";
    }

    @ResponseBody
    @RequestMapping("/doSaveJob")
    public String doSaveJob(String title, String contents, String date, String teacherName, String className) {
        int status = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(new Date()));
        System.out.println(date);
        Task task = new Task();
        task.setTask_name(title);
        task.setTask_content(contents);
        task.setTask_sta_time(df.format(new Date()));
        task.setTask_end_time(date);
        task.setCla_name(className);
        task.setT_name(teacherName);
        task.setStatus(status);
        taskService.addTask(task);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/notPublish")
    public String notPublish() {

        return "1";
    }

    @RequestMapping("/toNotPublish")
    public String toNotPublish() {

        return "teacher/not_publish";
    }

    @RequestMapping("/allTask")
    public String allTask(Model model) {
        List<Task> taskList = taskService.queryAllTaskByStatus(1);
        model.addAttribute("taskList", taskList);
        System.out.println(taskList.get(0).getTask_end_time());
        return "teacher/alltask";
    }

    @RequestMapping("/saveTask")
    public String saveTask(Model model) {
        List<Task> taskList = taskService.queryAllTaskByStatus(0);
        model.addAttribute("taskList", taskList);
        return "teacher/savetask";
    }

    @RequestMapping("/publishTask")
    public String publishTask() {
        return "teacher/publishtask";
    }

    @RequestMapping("/correctTask")
    public String correctTask(Model model) {
        List<Task> taskList = taskService.queryAllTaskByStatus(1);
        model.addAttribute("taskList", taskList);
        return "teacher/correct";
    }

    @RequestMapping("/deleteTask")
    public String deleteTask(int id) {
        taskService.deleteTask(id);
        return "redirect:/task/allTask";
    }

    @RequestMapping("/toUpdateTask")
    public String toUpdateTask(int id, Model model) {
        Task task = taskService.queryTaskById(id);
        model.addAttribute("task", task);
        return "teacher/updatetask";
    }

    @ResponseBody
    @RequestMapping("/doUpdateTask")
    public String doUpdateTask(String title, String contents, String date, String teacherName, String className, int taskid, int taskstatus) {
        Task task = new Task();
        task.setTask_name(title);
        task.setTask_content(contents);
        task.setTask_end_time(date);
        task.setT_name(teacherName);
        task.setCla_name(className);
        task.setTask_id(taskid);
        task.setStatus(1);
        taskService.updateTask(task);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/doUpdateTaskv")
    public String doUpdateTaskv(String title, String contents, String date, String teacherName, String className, int taskid, int taskstatus) {
        Task task = new Task();
        task.setTask_name(title);
        task.setTask_content(contents);
        task.setTask_end_time(date);
        task.setT_name(teacherName);
        task.setCla_name(className);
        task.setTask_id(taskid);
        task.setStatus(0);
        taskService.updateTask(task);
        return "1";
    }


}
